package com.org.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.org.context.BaseContext;
import com.org.entity.BackupCollectItems;
import com.org.entity.BackupSearchItems;
import com.org.mapper.BackupCollectItemsMapper;
import com.org.mapper.BackupSearchItemsMapper;
import com.org.properties.RedisKeyPropertis;
import com.org.service.BackupService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class BackupServiceImpl implements BackupService {

    private final BackupCollectItemsMapper backupCollectItemsMapper;

    private final BackupSearchItemsMapper backupSearchItemsMapper;

    private final StringRedisTemplate stringRedisTemplate;

    /**
     * 新增失物招领信息备份
     */
    @Override
    public void addCollectItems(BackupCollectItems collectItems) {
        BackupCollectItems backupCollectItems = new BackupCollectItems();
        BeanUtils.copyProperties(collectItems, backupCollectItems);
        backupCollectItems.setCreateTime(LocalDateTime.now());
        backupCollectItems.setUpdateTime(LocalDateTime.now());
        backupCollectItems.setOperatorId(BaseContext.getCurrentId());
        backupCollectItemsMapper.insert(backupCollectItems);
    }

    /**
     * 新增寻物启事信息备份
     */
    @Override
    public void addSearchItems(BackupSearchItems searchItems) {
        BackupSearchItems backupSearchItems = new BackupSearchItems();
        BeanUtils.copyProperties(searchItems, backupSearchItems);
        backupSearchItems.setCreateTime(LocalDateTime.now());
        backupSearchItems.setUpdateTime(LocalDateTime.now());
        backupSearchItems.setOperatorId(BaseContext.getCurrentId());
        backupSearchItemsMapper.insert(backupSearchItems);
    }

    /**
     * 删除所有备份信息
     */
    @Override
    public void deleteAll(LocalDateTime time) {
        if(time == null){
            List<BackupCollectItems> oldBackupCollectItems = backupCollectItemsMapper.selectList(null);
            //1、删除失物招领备份表
            int ifDeleteCollectItems = backupCollectItemsMapper.delete(null);
            if(ifDeleteCollectItems > 0){
                for(BackupCollectItems backupCollectItems : oldBackupCollectItems){
                    //删除Redis
                    stringRedisTemplate.opsForSet().remove(RedisKeyPropertis.REDIS_KEY_UP_USE, backupCollectItems.getImage());
                }
            }
            //2、删除寻物启事备份表
            List<BackupSearchItems> oldBackupSearchItems = backupSearchItemsMapper.selectList(null);
            int ifDeleteSeachItems = backupSearchItemsMapper.delete(null);
            if(ifDeleteSeachItems > 0){
                for(BackupSearchItems backupSearchItems : oldBackupSearchItems){
                    //删除Redis
                    stringRedisTemplate.opsForSet().remove(RedisKeyPropertis.REDIS_KEY_UP_USE, backupSearchItems.getImage());
                }
            }
        }else{
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

            List<String> collectItemsImageList = backupCollectItemsMapper.selectListByTime(time);
            //1、删除失物招领备份表
            int ifDeleteCollectItemsByTime = backupCollectItemsMapper.deleteByTime(time);
            if(ifDeleteCollectItemsByTime > 0){
                //删除Redis
                stringRedisTemplate.opsForSet().remove(RedisKeyPropertis.REDIS_KEY_UP_USE, collectItemsImageList.toArray());
                log.info("已删除{}之前的失物招领的备份数据",time.format(formatter));
            }

            List<String> searchItemsImageList = backupSearchItemsMapper.selectListByTime(time);
            //2、删除寻物启事备份表
            int ifDeleteSearchItemsByTime = backupSearchItemsMapper.deleteByTime(time);
            if(ifDeleteSearchItemsByTime > 0){
                //删除Redis
                stringRedisTemplate.opsForSet().remove(RedisKeyPropertis.REDIS_KEY_UP_USE, searchItemsImageList.toArray());
                log.info("已删除{}之前的寻物启事的备份数据",time.format(formatter));
            }
        }

    }
}
